package com.jeesite.modules.otheroutbound.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.otheroutbound.entity.OtherOutboundHeader;
import com.jeesite.modules.otheroutbound.dao.OtherOutboundHeaderDao;
import com.jeesite.modules.otheroutbound.entity.OtherOutboundLines;
import com.jeesite.modules.otheroutbound.dao.OtherOutboundLinesDao;

/**
 * 其它出库单Service
 * @author 码脑洞
 * @version 2025-04-13
 */
@Service
public class OtherOutboundHeaderService extends CrudService<OtherOutboundHeaderDao, OtherOutboundHeader> {
	
	@Autowired
	private OtherOutboundLinesDao otherOutboundLinesDao;
	
	/**
	 * 获取单条数据
	 * @param otherOutboundHeader
	 * @return
	 */
	@Override
	public OtherOutboundHeader get(OtherOutboundHeader otherOutboundHeader) {
		OtherOutboundHeader entity = super.get(otherOutboundHeader);
		if (entity != null){
			OtherOutboundLines otherOutboundLines = new OtherOutboundLines(entity);
			otherOutboundLines.setStatus(OtherOutboundLines.STATUS_NORMAL);
			entity.setLinesList(otherOutboundLinesDao.findList(otherOutboundLines));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param otherOutboundHeader 查询条件
	 * @param otherOutboundHeader page 分页对象
	 * @return
	 */
	@Override
	public Page<OtherOutboundHeader> findPage(OtherOutboundHeader otherOutboundHeader) {
		return super.findPage(otherOutboundHeader);
	}
	
	/**
	 * 查询列表数据
	 * @param otherOutboundHeader
	 * @return
	 */
	@Override
	public List<OtherOutboundHeader> findList(OtherOutboundHeader otherOutboundHeader) {
		return super.findList(otherOutboundHeader);
	}
	
	/**
	 * 查询子表分页数据
	 * @param otherOutboundLines
	 * @param otherOutboundLines page 分页对象
	 * @return
	 */
	public Page<OtherOutboundLines> findSubPage(OtherOutboundLines otherOutboundLines) {
		Page<OtherOutboundLines> page = otherOutboundLines.getPage();
		page.setList(otherOutboundLinesDao.findList(otherOutboundLines));
		return page;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param otherOutboundHeader
	 */
	@Override
	@Transactional
	public void save(OtherOutboundHeader otherOutboundHeader) {
		super.save(otherOutboundHeader);
		// 保存 OtherOutboundHeader子表
		for (OtherOutboundLines otherOutboundLines : otherOutboundHeader.getLinesList()){
			if (!OtherOutboundLines.STATUS_DELETE.equals(otherOutboundLines.getStatus())){
				otherOutboundLines.setHeaderId(otherOutboundHeader);
				if (otherOutboundLines.getIsNewRecord()){
					otherOutboundLinesDao.insert(otherOutboundLines);
				}else{
					otherOutboundLinesDao.update(otherOutboundLines);
				}
			}else{
				otherOutboundLinesDao.delete(otherOutboundLines);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param otherOutboundHeader
	 */
	@Override
	@Transactional
	public void updateStatus(OtherOutboundHeader otherOutboundHeader) {
		super.updateStatus(otherOutboundHeader);
	}
	
	/**
	 * 删除数据
	 * @param otherOutboundHeader
	 */
	@Override
	@Transactional
	public void delete(OtherOutboundHeader otherOutboundHeader) {
		super.delete(otherOutboundHeader);
		OtherOutboundLines otherOutboundLines = new OtherOutboundLines();
		otherOutboundLines.setHeaderId(otherOutboundHeader);
		otherOutboundLinesDao.deleteByEntity(otherOutboundLines);
	}
	
}